package com.pan.controller;


import com.pan.entity.Category;
import com.pan.entity.Result;
import com.pan.service.CategoryService;
import com.pan.vo.CategoryCreateVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @GetMapping("/select/id/{id}")
    @ResponseBody
    public Result select(@PathVariable("id") Long id){
        Result result = categoryService.selectById(id);
        return result;
    }

    @ResponseBody
    @GetMapping("/select/child/{id}")
    public Result<List<Category>> selectChildById(@PathVariable("id") Long id) {
        Result<List<Category>> result = categoryService.selectChildById(id);
        return result;
    }

    @GetMapping("/select/lv1")
    @ResponseBody
    public Result<List<Category>> selectByLevel1(){
        Result<List<Category>> result = categoryService.selectByLevel1();
        return result;
    }


    @PutMapping("/update/{updateName}")
    @ResponseBody
    public Result update(@CookieValue("adminTicket") String adminTicket,
                         @PathVariable("updateName") String updateName,
                         @RequestParam("id") Long id,
                         @RequestParam("updateValue") Object updateValue){
        Result result = categoryService.update(adminTicket,id,updateValue,updateName);
        return result;
    }

    @PostMapping("/insert")
    @ResponseBody
    public Result insert(@CookieValue("adminTicket") String adminTicket,
                         @RequestBody CategoryCreateVo categoryCreateVo){
        Result result = categoryService.insert(adminTicket,categoryCreateVo);
        return result;
    }

}
